<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>将 IPV4 的字符串互联网协议转换成长整型数字</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.ip2long" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">ip2long</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">ip2long</span> &mdash; <span class="dc-title">将 IPV4 的字符串互联网协议转换成长整型数字</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.ip2long-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>ip2long</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$ip_address</code></span>
   ) : <span class="type">int</span></div>

  <p class="para rdfs-comment">
   函数 <span class="function"><strong>ip2long()</strong></span> 返回 IPV4 网络地址的长整型格式，从标准网络地址格式(点字符串)转化得到。
  </p>
  <p class="para">
   <span class="function"><strong>ip2long()</strong></span> 还可以与非完整IP进行工作。
   阅读 <a href="http://publibn.boulder.ibm.com/doc_link/en_US/a_doc_lib/libs/commtrf2/inet_addr.htm" class="link external">&raquo;&nbsp;http://publibn.boulder.ibm.com/doc_link/en_US/a_doc_lib/libs/commtrf2/inet_addr.htm</a>
   获得更多信息。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.ip2long-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">ip_address</code></dt>

     <dd>

      <p class="para">
      一个标准格式的地址。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.ip2long-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回IP地址转换后的数字 或 <strong><code>FALSE</code></strong> 如果 <code class="parameter">ip_address</code>
   是无效的。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.ip2long-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.5.0</td>
       <td>
        Prior to this version, on Windows <span class="function"><strong>ip2long()</strong></span> would sometimes return
        a valid number even if passed a value which was not an (IPv4) Internet Protocol
        dotted address.
       </td>
      </tr>

      <tr>
       <td>5.2.10</td>
       <td>
        再此之前的版本, <span class="function"><strong>ip2long()</strong></span> 有时会返回即使这不是一个IPV4的标准地址的数字地址。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.ip2long-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5501">
    <p><strong>Example #1 <span class="function"><strong>ip2long()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$ip&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gethostbyname</span><span style="color: #007700">(</span><span style="color: #DD0000">'www.example.com'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$out&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"The&nbsp;following&nbsp;URLs&nbsp;are&nbsp;equivalent:&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$out&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">'http://www.example.com/,&nbsp;http://'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$ip&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'/,&nbsp;and&nbsp;http://'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%u"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">ip2long</span><span style="color: #007700">(</span><span style="color: #0000BB">$ip</span><span style="color: #007700">))&nbsp;.&nbsp;</span><span style="color: #DD0000">"/&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5502">
    <p><strong>Example #2 显示IP地址</strong></p>
    <div class="example-contents"><p>
     第二个例子说明打印一个转换后的地址使用
     <span class="function"><a href="printf.html" class="function">printf()</a></span> 在PHP4和PHP5的功能:
    </p></div>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$ip&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gethostbyname</span><span style="color: #007700">(</span><span style="color: #DD0000">'www.example.com'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$long&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">ip2long</span><span style="color: #007700">(</span><span style="color: #0000BB">$ip</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">$long&nbsp;</span><span style="color: #007700">==&nbsp;-</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">||&nbsp;</span><span style="color: #0000BB">$long&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Invalid&nbsp;IP,&nbsp;please&nbsp;try&nbsp;again'</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$ip&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;192.0.34.166<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">$long&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;-1073732954<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%u\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">ip2long</span><span style="color: #007700">(</span><span style="color: #0000BB">$ip</span><span style="color: #007700">));&nbsp;</span><span style="color: #FF8000">//&nbsp;3221234342<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.ip2long-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    因为PHP的 <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> 类型是有符号，并且有许多的IP地址讲导致在32位系统的情况下为负数， 你需要使用 &quot;%u&quot; 进行转换通过 <span class="function"><a href="sprintf.html" class="function">sprintf()</a></span> 或
    <span class="function"><a href="printf.html" class="function">printf()</a></span> 得到的字符串来表示无符号的IP地址。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    <span class="function"><strong>ip2long()</strong></span> 将返回 <strong><code>FALSE</code></strong> 在IP是
    <em>255.255.255.255</em> 的情况，版本为 PHP 5 &lt;= 5.0.2. 在修复后
    PHP 5.0.3 会返回 <em>-1</em> (与PHP4相同).
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.ip2long-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="long2ip.html" class="function" rel="rdfs-seeAlso">long2ip()</a> - 将长整型转化为字符串形式带点的互联网标准格式地址（IPV4）</span></li>
    <li class="member"><span class="function"><a href="sprintf.html" class="function" rel="rdfs-seeAlso">sprintf()</a> - Return a formatted string</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>